package org.hxyjs.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import org.hxyjs.dto.DTO;
import org.hxyjs.entity.EventDetails;
import org.hxyjs.page.PageInfo;
import org.hxyjs.service.EventDetailsService;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;


@CrossOrigin
@RestController //@Controller注解是用spring容器生成 对象 默认对象名 eventdetailsController
 //@RequestMapping 用户请求查找该资源使用的名字 网名 默认  /eventdetailsController
public class EventDetailsController {
	@Resource
	EventDetailsService eventdetailsService;
	@GetMapping("/eventdetailss")
	@ResponseBody //把方法的返回值按json转换之后 输出到浏览器
	public DTO<EventDetails> getAllEventDetailss() throws Exception{
		System.out.println("查询全部");
		return eventdetailsService.getAllEventDetailss();
	}

	@RequestMapping("/getEventDetailsByUno/{uno}")
	@ResponseBody //把方法的返回值按json转换之后 输出到浏览器
	public DTO<EventDetails> getEventDetailsByUno(@PathVariable("uno") String uno) throws Exception{
		System.out.println(uno + "查询成功");
		return eventdetailsService.getEventDetailsByUno(uno);
	}
	//@SystemMail(op = "EventDetails:addEventDetails",queueName = "family.mail",module = "用户管理",methods = "用户注册")
	@PostMapping("/eventdetails")
	@ResponseBody
	public DTO<EventDetails> addEventDetailsByUno(@RequestBody EventDetails eventdetails) throws Exception{//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println("增加成功");
		return eventdetailsService.addEventDetailsByUno(eventdetails);
	}

	@DeleteMapping("/deleteEventDetails/{uno}")
	@ResponseBody
	public DTO<EventDetails> deleteEventDetails(@PathVariable("uno") String uno) throws Exception{//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println(uno+"删除成功");
		return eventdetailsService.deleteEventDetails(uno);
	}

	@RequestMapping("/getEventDetailssWithOrders/{uno}")
	@ResponseBody
	public DTO<EventDetails> getEventDetailssWithOrders(@PathVariable("uno") String uno,HttpServletRequest request,HttpServletResponse response) throws Exception{//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println("查询学生选课111");
		DTO<EventDetails> dto = eventdetailsService.getEventDetailssWithOrders(uno);
		saveSelectCourseCookieProcess(dto.getT(),request,response);
		return  dto;
	}
	@PatchMapping(value = "/eventdetails",headers = {"content-type=application/json"},consumes = MediaType.APPLICATION_JSON_VALUE
	)//部分修改 //部分修改
	public DTO<EventDetails> updateEventDetails(@RequestBody EventDetails eventdetails) throws Exception{//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println("修改成功");
		return eventdetailsService.updateEventDetails(eventdetails);
	}
//	@RequestMapping("/eventdetailss/pageInfo")
	@GetMapping("/getEventDetailssByPage")
public DTO<EventDetails> getEventDetailssByPage(@SpringQueryMap PageInfo pageInfo) throws Exception {//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println("查询分页");
		return eventdetailsService.getEventDetailssByPage(pageInfo);
	}
	public DTO<EventDetails> saveSelectCourseCookieProcess(EventDetails eventdetails, HttpServletRequest request, HttpServletResponse response) throws JsonProcessingException, UnsupportedEncodingException {
		//cookie是以key value形式存放在浏览器的文本
		DTO dto = new DTO(200,"cookie保存成功");
//		if(eventdetails!=null&&eventdetails.getUno()!=null) {
//			ObjectMapper objectMapper = new ObjectMapper();
//			Optional<List<OrderForm>> orderFormsOp = Optional.ofNullable(eventdetails.getOrderForms());
//			List<OrderForm> orderForms = null;
//			if (orderFormsOp.isPresent()&&orderFormsOp.get().size()>0) {
//				orderForms = new ArrayList<OrderForm>(eventdetails.getOrderForms());
//
//				for (OrderForm orderForm : eventdetails.getOrderForms()) {
//					Cookie orderFormsCookie = new Cookie("eventdetails" + eventdetails.getUno() + "_" + orderForm.getUrno(), URLEncoder.encode(objectMapper.writeValueAsString(orderForm), "UTF-8"));
//
//					//eventdetailsCookie.setMaxAge(30*24*60*60);   //存活期为一个月 30*24*60*60
//					orderFormsCookie.setPath("/");
//					orderFormsCookie.setDomain("localhost");
//					orderFormsCookie.setMaxAge(2 * 24 * 60 * 60);   //存活期为2天 2*24*60*60
//					response.addCookie(orderFormsCookie);
//
//				}
//			}
//			eventdetails.setOrderForms(null);
//			Cookie eventdetailsCookie = new Cookie("eventdetails" + eventdetails.getUno(), URLEncoder.encode(objectMapper.writeValueAsString(eventdetails), "UTF-8"));
//			eventdetailsCookie.setPath("/");
//			eventdetailsCookie.setDomain("localhost");
//			eventdetailsCookie.setMaxAge(2 * 24 * 60 * 60);   //存活期为2天 2*24*60*60
//			response.addCookie(eventdetailsCookie);
//			eventdetails.setOrderForms(orderForms);
//		}else{
//			dto.setCode(406);
//			dto.setMsg("缺少必要的参数，非法操作");
//		}
		return dto;
	}



	@RequestMapping("/activate/{uno}")
	@ResponseBody
	public DTO<EventDetails> activate(@PathVariable("uno") String uno) throws Exception{

		return eventdetailsService.activate(uno);
	}


	@RequestMapping("/lettory/{uno}/{giftName}")
	@ResponseBody
	public DTO<EventDetails> lettory(@PathVariable("uno") String uno,@PathVariable("giftName")String giftName) throws Exception{

		return eventdetailsService.lettory(uno,giftName);
	}



}
